﻿namespace Probel.Client.Commands
{
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;

    [Command("help", "h", "Displays this help menu")]
    public class HelpCommand : ICommand
    {
        #region Methods

        public void Execute(dynamic context)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Supported commands:");
            Console.ResetColor();

            var @namespace = "Probel.Client.Commands";
            var types = (from t in Assembly.GetExecutingAssembly().GetTypes()
                         where t.IsClass && t.Namespace == @namespace
                         select t.GetCustomAttributes<CommandAttribute>().FirstOrDefault());
            types = types.Where(e => e != null).OrderBy(e => e.Name);

            foreach (var type in types) { Display(type); }
        }

        private void Display(CommandAttribute type)
        {
            Console.WriteLine("{0,15} [{1, -3}] - {2} ", type.Name, type.ShortName, type.Description);
        }

        #endregion Methods
    }
}